package com.easyfun.healthmagicbox.base;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.kuaipan.android.openapi.KuaiPanManager;
import cn.sharesdk.framework.ShareSDK;

import com.easyfun.healthmagicbox.HMBMainActivity;
import com.easyfun.healthmagicbox.HealthBoxApplication;
import com.easyfun.healthmagicbox.ProfileActivity;
import com.easyfun.healthmagicbox.R;
import com.easyfun.healthmagicbox.SettingActivity;
import com.easyfun.healthmagicbox.pojo.ConstantData;
import com.easyfun.healthmagicbox.sync.HealthService;
import com.easyfun.healthmagicbox.sync.SyncManager;

public class BaseActivity extends Activity{
		
	private static final String TAG = BaseActivity.class.getSimpleName();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
	}
	
	protected String m_personid;
	protected String m_doctorid;

	protected String getPersonid() {
		return m_personid;
	}
	
	protected String getDoctorid() {
		return m_doctorid;
	}

	/**
	 * this activity need the person ID
	 * @return
	 */
	protected boolean checkPersonId()
	{
		
		
		m_personid = getIntent().getStringExtra(ConstantData.HMBPERSONID);
		// 
		if ((m_personid == null) || (m_personid.length() == 0)) {
			setResult(RESULT_CANCELED);
			finish();
			return false;
		}
		return true;
	}
	
	
	protected void addbackButton(final Activity activity)
	{
		/*
		TextView txtGohome = (TextView) findViewById(R.id.txt_gohome);
		if (txtGohome!=null)
		{
			txtGohome.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {

					final Intent intenthome = new Intent(activity, HMBMainActivity.class);
		    	
					activity.startActivity(intenthome);//
				}
			});
		}
		*/
		RelativeLayout Goback = (RelativeLayout) findViewById(R.id.leftpress);
		if (Goback != null)
			Goback.setOnClickListener(new OnClickListener() {
			
				@Override
				public void onClick(View v) {
					activity.finish();
				
				}
			});
		ImageButton txtGoback = (ImageButton) findViewById(R.id.title_btn); //txt_goback);
		if (txtGoback!=null)
		{
			txtGoback.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					activity.finish(); 
					
				}
			});
		}
	}
	/**
	 * this activity need the person ID
	 * @return
	 */
	protected boolean checkDoctorId()
	{
		m_doctorid = getIntent().getStringExtra(ConstantData.DOCTORID);
		// 
		if ((m_doctorid == null) || (m_doctorid.length() == 0)) {
			setResult(RESULT_CANCELED);
			finish();
			return false;
		}
		return true;
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.hmbmain, menu);
		return false;
	}
	  
    public boolean onOptionsItemSelected(MenuItem item, Activity activity) {
    	switch (item.getItemId()) {  
        case R.id.menu_gohome:
        	final Intent intenthome = new Intent(activity, HMBMainActivity.class);
        	    	
        	this.startActivity(intenthome);//
        	break;
        case R.id.menu_setting:  
        	final Intent intent = new Intent(activity, SettingActivity.class);
        	        	
			this.startActivity(intent);//
            break;  
        case R.id.menu_exit:  
        	SharedPreferences userinfo = this.getSharedPreferences("UserInfo", MODE_PRIVATE);
        	
        	Editor editor = userinfo.edit();
        	editor.putString("STRING_SW", "");
			editor.putBoolean("remautologin", false);
			editor.commit();
        	//android.os.Process.killProcess(android.os.Process.myPid());//获取PID
			stopApp();
        	System.exit(0);
            break; 
        }  
        return super.onOptionsItemSelected(item); 
    }

	@Override  
    public boolean onOptionsItemSelected(MenuItem item) {  
		super.onOptionsItemSelected(item);
         return false;
    }  
	
	public void stopApp()
	{
		KuaiPanManager.getInstance(this).logOut();
		try
		{
			ShareSDK.stopSDK(this);
		}catch (Exception e)
		{
			e.printStackTrace();
			Log.e(TAG,e.toString());
		}
		SyncManager.getInstance().stop();
		stopService(new Intent(this, HealthService.class));
	    int currentVersion = android.os.Build.VERSION.SDK_INT;  
        if (currentVersion > android.os.Build.VERSION_CODES.ECLAIR_MR1) {  
            Intent startMain = new Intent(Intent.ACTION_MAIN);  
            startMain.addCategory(Intent.CATEGORY_HOME);  
            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
            startActivity(startMain);  
            System.exit(0);  
        } else {// android2.1  
            ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);  
            am.killBackgroundProcesses(getPackageName());  
        }  

	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if(keyCode == KeyEvent.KEYCODE_BACK) {
	    	this.setResult(RESULT_OK);
	    	this.finish();
	    }
		return super.onKeyDown(keyCode, event);
	}
}
